//
//  ActivityResultController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/26.
//  Copyright © 2016年 czd. All rights reserved.
//
#define DEVICEWIDTH [UIScreen mainScreen].bounds.size.width
#define DEVICEHEIGHT [UIScreen mainScreen].bounds.size.height
#import "ActivityResultController.h"
#import "GroupGoingModel.h"
#import "ActivityViewCell.h"

@interface ActivityResultController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) UICollectionView *collec;

@property (nonatomic,assign) NSInteger currentPage;
@end

@implementation ActivityResultController
static NSString *const reuserID = @"ActivityResultCell";

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collec = [[UICollectionView alloc] initWithFrame:CGRectMake(10,10,DEVICEWIDTH - 20 , DEVICEHEIGHT-218) collectionViewLayout:layout];
    layout.minimumLineSpacing = 10;
    collec.dataSource = self;
    collec.delegate = self;
    self.collec = collec;
    
    [collec registerNib:[UINib nibWithNibName:@"ActivityViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuserID];
    
    collec.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    [self.view addSubview:collec];
    
    __weak typeof (self) weakSelf = self;
    
    [self.collec addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.collec headerBeginRefreshing];
    
    [self.collec addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];

}

- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyzmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            GroupGoingModel *model = [GroupGoingModel mj_objectWithKeyValues:dict2];
            [self.dataArray addObject:model];
            
        }
        [self.collec headerEndRefreshing];
        [self.collec reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

- (void)MoreData{
    
    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyzmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            GroupGoingModel *model = [GroupGoingModel mj_objectWithKeyValues:dict2];
            [self.dataArray addObject:model];
            
        }
        [self.collec footerEndRefreshing];
        
        [self.collec reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GroupGoingModel *model = self.dataArray[indexPath.row];
    
    ActivityViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuserID forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [ActivityViewCell WithActivityViewCell];
    }
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.model = model;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(DEVICEWIDTH - 20,(DEVICEHEIGHT - 218)/3);
}


@end












